package database

import exceptions.BlackListException

object UserMgr {
    private val blackList = hashSetOf<Long>()

    @JvmStatic
    fun initUser(userId: Long): User {
        if (userId in blackList) {
            throw BlackListException()
        }

        val conn = DBConnMgr.getConn()
        val query = conn.prepareStatement("SELECT * FROM table_users WHERE user_id=?;").apply {
            setLong(1, userId)
            executeQuery()
        }
        val resultSet = query.resultSet
        while (resultSet.next()) {
            val user = User(
                userId = resultSet.getLong("user_id"),
                countPick = resultSet.getInt("count_pick"),
                countSend = resultSet.getInt("count_send")
            )
            resultSet.close()
            query.close()
            return user
        }
        conn.prepareStatement("INSERT INTO table_users (user_id, count_send, count_pick) VALUES (?,0,0);").apply {
            setLong(1, userId)
            executeUpdate()
            close()
        }
        return User(userId = userId, countSend = 0, countPick = 0)
    }
}